package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.constant.ArtifactStatus;
import com.rmt.www.dao.DesignArtifactDao;
import com.rmt.www.vo.Artifact;
import com.rmt.www.vo.DesignArtifact;

public class DesignArtifactDaoImpl implements DesignArtifactDao {

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void deleteDesignArtifactById(int aid) {
		jdbcTemplate.update("update designartifact set status=? where id=?",
				new Object[]{ArtifactStatus.DELETED, aid},
				new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
	}

	public DesignArtifact getDesignArtifactById(int aid) {
		try {	
			return  (DesignArtifact) this.jdbcTemplate.queryForObject("select * from designartifact where id = ? and status != ?",
					new Object[]{aid, ArtifactStatus.DELETED}, new DesignArtifactRowMapper());
		}catch (final EmptyResultDataAccessException e) {
			  return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<DesignArtifact> getDesignArtifactsByPId(int pid) {
		return (List<DesignArtifact>)(this.jdbcTemplate.query("select * from designartifact where pid=? and status != ?",
				new Object[]{pid, ArtifactStatus.DELETED}, 
				new int[]{ java.sql.Types.INTEGER, java.sql.Types.VARCHAR}, new DesignArtifactRowMapper()));
	}

	public int insertDesignArtifact(DesignArtifact artifact) {
		jdbcTemplate.update("insert into designartifact(title,architect,consideration,input,function,output,perfrequest,securityrequest,storagerequest,innerinterface,outerinterface,pid,priority,effort,cost,risk,comment,createrid,createdtime,updaterid,updatedtime,version,status,updatedtimes)"+
				"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",  
				new Object[] {
				artifact.getTitle(),
				artifact.getArchitect(),
				artifact.getConsideration(),
				artifact.getInput(),
				artifact.getFunction(),
				artifact.getOutput(),
				artifact.getPerfrequest(),
				artifact.getSecurityrequest(),
				artifact.getStoragerequest(),
				artifact.getInnerinterface(),
				artifact.getOuterinterface(),
				artifact.getPid(),
				artifact.getPriority(),
				artifact.getEffort(),
				artifact.getCost(),
				artifact.getRisk(),
				artifact.getComment(),
				artifact.getCreaterid(),
				artifact.getCreatedtime(),
				artifact.getUpdaterid(),
				artifact.getUpdatedtime(),
				artifact.getVersion(),
				artifact.getStatus(),
				artifact.getUpdatedtimes()
		}, 	new int[] {
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.INTEGER,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER
				
		});
		
		return jdbcTemplate.queryForInt("select last_insert_id()");
	}

	public void updateDesignArtifact(DesignArtifact artifact) {
		jdbcTemplate.update("update designartifact set title=?,architect=?,consideration=?,input=?,function=?,output=?,perfrequest=?,securityrequest=?,storagerequest=?,innerinterface=?,outerinterface=?,pid=?,priority=?,effort=?,cost=?,risk=?,comment=?,createrid=?,createdtime=?,updaterid=?,updatedtime=?,version=?,status=?,updatedtimes=?", 
				new Object[] {
				artifact.getTitle(),
				artifact.getArchitect(),
				artifact.getConsideration(),
				artifact.getInput(),
				artifact.getFunction(),
				artifact.getOutput(),
				artifact.getPerfrequest(),
				artifact.getSecurityrequest(),
				artifact.getStoragerequest(),
				artifact.getInnerinterface(),
				artifact.getOuterinterface(),
				artifact.getPid(),
				artifact.getPriority(),
				artifact.getEffort(),
				artifact.getCost(),
				artifact.getRisk(),
				artifact.getComment(),
				artifact.getCreaterid(),
				artifact.getCreatedtime(),
				artifact.getUpdaterid(),
				artifact.getUpdatedtime(),
				artifact.getVersion(),
				artifact.getStatus(),
				artifact.getUpdatedtimes()
		}, 	new int[] {
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.INTEGER,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER
				
		});
	}

}
